#!/bin/sh

#clear
alias curl='curl -stderr'
export _eval=tmp$$

quote=`./quote.sh $1|cut -d':' -f2|awk '{print $1}'`
echo $1=$quote

\rm -f digest*
export submit="submit=submit&symbol=$1"
curl -stderr -d $submit "http://www.datawm.com/stockdigest/stockdigest.do" > digest$$ &

#MSN StockScouter
curl "http://moneycentral.msn.com/investor/StockRating/srsmain.asp?Symbol=$1" > $_eval
export MSNStockScouter=`cat $_eval |egrep  "SRS[0-9]" |cut -d"." -f1|cut -d"/" -f2 |cut -c 4-5`
export Fudamental=`cat $_eval |grep -i Fundamental |grep -o ">[A-F]<" |cut -c 2`
export Ownership=`cat $_eval |grep -i Ownership |grep -o ">[A-F]<" |cut -c 2`
export Valuation=`cat $_eval |grep -i Valuation |grep -o ">[A-F]<" |cut -c 2`
export Technical=`cat $_eval |grep -i Technical |grep -o ">[A-F]<" |cut -c 2`
echo -e "MSN Rating:\t"$MSNStockScouter


#Power Rating
export _powerrating=`curl -stderr -b "CFGLOBALS=HITCOUNT%3D6%23LASTVISIT%3D%7Bts+%272007%2D01%2D31+22%3A49%3A19%27%7D%23TIMECREATED%3D%7Bts+%272006%2D10%2D14+00%3A08%3A00%27%7D%23; __utma=37317605.857987777.1160798888.1160800866.1170301752.3; CFID=31721102; CFMAGIC=31721102%3A81490065; CFTOKEN=81490065; __utmz=37317605.1160798888.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); SESSIONID=01311058; VIEWED=000; __utmb=37317605; __utmc=37317605; SOURCE=URL%5Ftradingmarkets" -d "symbol=$1" http://www.tradingmarkets.com/.site/powerratings |egrep -o "[0-9].swf" |head -n 1 | cut -d'.' -f1`
echo -e "Power Rating:\t"$_powerrating

#Crammer's MadMoney comments
export madmoneyurl="http://madmoney.thestreet.com/index.cfm?page=lookup"
export madmoneylookup="symbol=$1"
export madmoneyreturn=`curl -d $madmoneylookup $madmoneyurl |egrep -m 1  ">[0-9]{2}/[0-9]{2}/200?" -A 3` #care the latest one
export maddate=`echo $madmoneyreturn |egrep -o "[0-9]{2}/[0-9]{2}/200[0-9]"`
export madbuysell=`echo $madmoneyreturn |egrep -o "[1-5]\.gif"|sed 's/.gif//g' |sed 's/5/SB/g'|sed 's/4/B/g'|sed 's/3/H/g'|sed 's/2/S/g'| sed 's/1/SS/g'`
export madvalue=`echo $madmoneyreturn |egrep -o "[0-9]+\.[0-9]+"`
echo -e "Crammer:\t"$maddate $madbuysell $madvalue 

#StoackTA analysis
export stocktaoverall=`curl "http://www.stockta.com/cgi-bin/analysis.pl?symb=$1&num1=2&cobrand=&mode=stock" |grep Short -A 1 |egrep -o ">[A-Za-z]+.[A-Za-z]+" | sed 's/>//g' |tail -n 4 |head -n 1`
echo -e "TA Trend:\t"$stocktaoverall
curl -stderr "http://download.finance.yahoo.com/d/quotes.csv?s=$1,SPY&f=m8" |sed 's/%//g' |dos2unix |tr '\n' ' ' |awk '{printf("50-MA vs. S&P\t%3.2f%\n",$1-$2)}'

#Stock Pick Rating
export tmp=`curl http://www.stockpickreport.com/abrating.php?sym=$1`
export stockpickreport=`echo $tmp |egrep -o "ratingmeter/[a-zA-Z]+\.gif" |cut -d/ -f2- |cut -d. -f1`
echo -e "Stock Pick:\t"$stockpickreport

#stoxline rating
export stoxline=`curl -stderr "http://www.stoxline.com/quote.php?s=$1" |egrep -o "[1-5]s.bmp" |sed 's/s.bmp//g' |sed -e 's/1/Strong Sell/g' -e 's/2/Sell/g'  -e 's/4/Neutral/g' -e 's/3/Buy/g' -e 's/5/Strong Buy/g'`
echo -e "Stoxline:\t"$stoxline

#MotleyFool's rating
export star=`curl -stderr "http://caps.fool.com/Ticker.aspx?ticker=$1" |grep -A 2 "CAPS Rating:" |egrep -o "[a-z_]+.gif" |cut -d'.' -f1 |cut -d'_' -f3`
echo -e "MotelyFool:\t"$star

#TraderStar 
TraderStar=`curl -stderr "http://www.tradestars.com/grail-indicator.asp?symbol=$1" |grep "*FREE SIGNALS" |sed 's/<br>/\n/g' |head -n 5 |egrep -o "NO Signal|go SHORT|go LONG"`
echo -e "TraderStar:\t"$TraderStar

#Trend Spotter
trenspotter=`curl -stderr "http://www.stockta.com/cgi-bin/opinion.pl?symb=$1&num1=4&mode=stock"|sed 's/TR/\n/g' |grep "Trend Spotter"  |egrep -o ">Buy<|>Sell<|>Hold<" |sed -e 's/>//g' -e 's/<//g'`
echo -e "Trend Spotter:\t"$trenspotter

#Thestreet.com rating
upper=`echo $1|tr [a-z] [A-Z]`
thestreet=`curl -stderr "http://www.stockpickr.com/symbol/$upper/" |egrep -o "Rating: [A|B|C|D|N]" |sed -e 's/Rating: //g' -e 's/N/No Rating/g'`
numpro=`curl -stderr "http://www.stockpickr.com/symbol/$upper/" |grep ">Pro<" |wc -l`
echo -e "TheStreet.com:\t"$thestreet," Owned by # Pro:"$numpro


echo "===========================Covestor Holdings==========================="
echo "==========member       return     dir   date  rank  overall========"
#member;annualized return; Purchase date; Rank; SharpeRatio
curl -stderr "http://www.covestor.com/rankings/portfolio?showselector=1" |egrep -o "<td>[0-9]+.[0-9]+</td>" |cat -n |sed -e 's/<td>//g' -e 's/<\/td>//g' > tmp1$$
curl -stderr "http://www.covestor.com/rankings/portfolio?showselector=1" |egrep -o "/mbr/[a-zA-Z0-9_.-]+" |sed 's/ //g' |uniq |cat -n > tmp2$$
join tmp1$$ tmp2$$ > tmprank$$ #Rank List

lower=`echo $1 |tr [A-Z] [a-z]`
thismonth=`date +%b`
lastmonth=`date -d-30day +%b`
curl "http://www.covestor.com/stk/$lower/holders" |egrep  -o "/mbr/[a-z0-9A-Z_.-]+/blog\?stk=[a-z]+|[0-9]+.[0-9]+%|>-[0-9]+.[0-9]+%|[0-9]+-[A-Z][a-z]{2}-[0-9]{2}&nbsp;<|n/a|long_market|short_market|prevHolders"|sed -e 's/100%//g'  -e 's/>//g' -e 's/&nbsp;//g' -e 's/_market//g' -e 's/\/mbr//g' -e 's/blog?stk=//g' |uniq |egrep -B 1000 prevHolders |grep -v prevHolders  |tr '\n' ' '|tr '<' '\n' |egrep "$thismonth|$lastmonth" |awk '{printf("%30s\t%3.2f%\t%5s\t%10s\n",$1,$2,$3,$4)}' > tmp$$

cat tmp$$ |while read line
do
    member=`echo $line |cut -d'/' -f2`
    rank=`grep $member tmprank$$ |awk '{print $1,$2}'`
    echo $line $rank |awk '{printf("%20s\t%3.2f%\t%5s\t%10s\t%3d\t%2.2f%\n",$1,$2,$3,$4,$5,$6)}'
done
\rm -f tmp1$$ tmp2$$ tmprank$$

echo "Social Picker Ratings=================="
curl "http://www.socialpicks.com/stock/$1/sentiment" |egrep -o "graphic_rating_buy|graphic_rating_sell|graphic_rating_hold|[0-9]{2}/[0-9]{2}/[0-9]{2}|lightBlack>[0-9].[0-9]<|>Pro<" |sed -e 's/lightBlack//g' -e 's/>//g' -e 's/<//g' -e 's/graphic_rating_//g' |tr '\n' ' ' |sed -e 's/buy/\nbuy/g' -e 's/hold/\nhold/g' -e 's/sell/\nsell/g' |sed  '/^$/d' |head -n 5 > tmp$$
cat tmp$$ |while read line
do
    date=`echo $line |awk '{print $2}'`
    price=`./quote.sh $1 $date`
    echo $line $price
done

echo "ETrade================================"
./etrade-research.sh $1

#Fidelity Research
echo "Fidelity================================"
./fidelity-research.sh  $1

while [ `cat digest$$|wc -c` -eq 0 ]
do 
    sleep 1
done
#zack=`cat digest$$ |grep -A 2 Zacks |tail -n 1 |cut -d']' -f2 |cut -d'<' -f1`
zack=`cat digest$$ |grep -A 2 Zacks |head -n 3 |cut -d']' -f2 |cut -d'<' -f1 |tail -n 1`
echo "ZACKS================================"
echo $zack

